//
//  UserLoginViewController.m
//  GC_OC
//
//  Created by Tianbao Wang on 2022/5/27.
//

#import "UserLoginViewController.h"
#import "LoginTextfieldView.h"
#import "Cache.h"
#import "MineViewController.h"
#import "RegisterViewController.h"
#import "AccountverifyViewController.h"
#import "LoginViewController.h"

@interface UserLoginViewController ()
@property(nonatomic, strong) UIView *navigationBarView;
@property(nonatomic, strong) LoginTextfieldView *accountView;
@property(nonatomic, strong) LoginTextfieldView *pwdView;
@property (nonatomic, strong) ObjectAFNetWorking* objectAFNet;
@property (nonatomic, strong) NSString* loginCount;
@property (nonatomic, strong) UIButton *loginButton;

@end

@implementation UserLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.objectAFNet = [ObjectAFNetWorking new];
    [self setupUIView];
}

//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    [self.navigationController setNavigationBarHidden:true animated:true];
//    
//}
//
//- (void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:animated];
//    [self.navigationController setNavigationBarHidden:false animated:true];
//    
//}

- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)regAct {
    [self.navigationController pushViewController:[RegisterViewController new] animated:YES];
}

- (void)forgetAct {
    [self.navigationController pushViewController:[AccountverifyViewController new] animated:YES];
}

- (void)loginAct {
    if ([self.accountView.textfield.text length] == 0) {
        [self.view makeToast:@"请输入账号"];
        return;
    }
    if ([self.pwdView.textfield.text length] == 0) {
        [self.view makeToast:@"请输入密码"];
        return;
    }
    
    // 统计当前账号在今天的登录失败的次数
    self.loginCount = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@_loginCount_%@",self.accountView.textfield.text,[NSString getYYMMDD:[NSDate date]]]];
    if (!self.loginCount) {
        self.loginCount = @"0";
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"account"] = self.accountView.textfield.text;
    param[@"password"] = self.pwdView.textfield.text;
    param[@"version"] = [UIApplication sharedApplication].fk_appVersion;
    [FKHttpTool postWithSuffix:@"/api/user/login" param:param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            UserModel *user = [UserModel mj_objectWithKeyValues:result.data];
            
            [Cache shared].userInfo = user;
            [FKCacheTool saveUserInfo:result.data];
            [FKCacheTool saveToken:user.userinfo.token];
            [SVProgressHUD showSuccessWithStatus:@"登录成功"];
            
            [self.navigationController dismissViewControllerAnimated:true completion:^{
                [[NSNotificationCenter defaultCenter] postNotificationName:@"loginSuccess" object:nil userInfo:nil];
            }];
        } else {
            if ([result.code isEqualToString:@"403"]) {
                self.loginCount = [NSString stringWithFormat:@"%d",[self.loginCount intValue]+1];
                if ([self.loginCount intValue] == 5) {
                    self.loginButton.backgroundColor = [UIColor grayColor];
                    self.loginButton.userInteractionEnabled = NO;
                }
            }
            [self.view makeToast:result.msg];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.view makeToast:error.debugDescription];
    }];
}

- (void)setupUIView {
    self.view.backgroundColor = UIColor.whiteColor;
    UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_bg"]];
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    self.navigationBarView = [UIView new];
    self.navigationBarView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.navigationBarView];
    [self.navigationBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(NAVI_BAR_HIGHT);
    }];
    
    UILabel *naviTitle = [UILabel new];
    naviTitle.text = @"用户登录";
    naviTitle.textAlignment = NSTextAlignmentCenter;
    naviTitle.textColor = [UIColor blackColor];
    naviTitle.font = [UIFont systemFontOfSize:20 weight:UIFontWeightMedium];
    [self.navigationBarView addSubview:naviTitle];
    [naviTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.navigationBarView);
        make.top.mas_equalTo(StatusBarHeight);
        make.height.mas_equalTo(40);
    }];
    
    UIButton *backButton = [UIButton new];
    [backButton setImage:[UIImage imageNamed:@"icon_返回_32_left"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationBarView addSubview:backButton];
    [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(32);
        make.left.equalTo(self.view).offset(12);
        make.top.mas_equalTo(StatusBarHeight + 4);
    }];
    
    UIImageView *logoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
    [self.view addSubview:logoView];
    [logoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(160);
    }];
    
    self.accountView = [[LoginTextfieldView alloc] init];
    self.accountView.nameLabel.text = @"账号";
    [self.view addSubview:self.accountView];
    [self.accountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(logoView.mas_bottom).offset(72);
        make.height.mas_equalTo(34);
    }];
    
    self.pwdView = [[LoginTextfieldView alloc] init];
    self.pwdView.nameLabel.text = @"密码";
    self.pwdView.isPwd = YES;
    self.pwdView.textfield.secureTextEntry = YES;
    [self.view addSubview:self.pwdView];
    [self.pwdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.accountView.mas_bottom).offset(15);
        make.height.mas_equalTo(34);
    }];
    
    
    self.loginButton = [UIButton new];
    self.loginButton.clipsToBounds = YES;
    self.loginButton.layer.cornerRadius = 28;
    self.loginButton.backgroundColor = XHUIColorFromRGB(0x0091FF);
    [self.loginButton setTitle:@"登陆" forState:UIControlStateNormal];
    self.loginButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC" size: 16];
    [self.loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.loginButton addTarget:self action:@selector(loginAct) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.loginButton];
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(56);
        make.right.equalTo(self.view).offset(-50);
        make.left.equalTo(self.view).offset(50);
        make.top.equalTo(self.pwdView.mas_bottom).offset(96);
    }];
    self.loginCount = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@_loginCount_%@",self.accountView.textfield.text,[NSString getYYMMDD:[NSDate date]]]];
    if ([self.loginCount intValue] == 5) {
        self.loginButton.backgroundColor = [UIColor grayColor];
        self.loginButton.userInteractionEnabled = NO;
    }
    
    UIButton *regButton = [UIButton new];
    [regButton setTitle:@"注册用户" forState:UIControlStateNormal];
    regButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC" size: 16];
    [regButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [regButton addTarget:self action:@selector(regAct) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:regButton];
    [regButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(50);
        make.bottom.equalTo(self.view).offset(-38);
    }];
    
    UIButton *forgetButton = [UIButton new];
    [forgetButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    forgetButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC" size: 16];
    [forgetButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [forgetButton addTarget:self action:@selector(forgetAct) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetButton];
    [forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-50);
        make.bottom.equalTo(self.view).offset(-38);
    }];
    

}

@end
